﻿/**
 *    Copyright 2012 Yohan Courbe
 *
 *  Licensed under the Apache License, Version 2.0 (the "License 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *      
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using System.Text;
using System.Text.RegularExpressions;
using NetworkAnalyzer.Engines.TcpSessionTracker;
using PacketDotNet;
using NetworkAnalyzer.Engines.Graphics.HttpHits;

namespace NetworkAnalyzer.Engines.HttpTracker
{
    class HttpTrackerEngine : Engine
    {
        public HttpTrackerEngine()
        {
            Console.WriteLine("{0} Started", Name);
            //_graphicsEngine = (GraphicsEngine) EnginesManager.GetEngineByName("Graphics");
        }

        public override void AddIpTcpPacket(Packet packet, IpPacket ipPacket, TcpPacket tcpPacket)
        {
            if (tcpPacket.DestinationPort != 80 && tcpPacket.SourcePort != 80) return;
            if (!tcpPacket.Psh) return;
            //var test = (TcpSessionTrackerEngine) EnginesManager.GetEngineByName("TcpSessionsTracker");
            var data = Encoding.ASCII.GetString(tcpPacket.PayloadData);

            if (data.IndexOf('\n') < 0) return;

            var lines = data.Split('\n');

            var httpRequestMatches = Regex.Match(lines[0], @"GET (.+) HTTP");
          
            if (httpRequestMatches.Success)
            {
                var httpRequest = new HttpRequest(HttpRequestQuery.Get, httpRequestMatches.Groups[1].Value);
                httpRequest.ParseLines(lines);
                
                var hostname = httpRequest.GetHttpHeaderFieldByName("host");
                //GraphicsHttpHits.AddHit(hostname.Value);
                
                var tmp = Regex.Match(hostname.Value, @"(.+\.)*(.+\..+)");
                GraphicsHttpHits.AddHit(tmp.Groups[2].Value);
                //Console.WriteLine("{1} - {0}", tmp.Groups[2].Value, ipPacket.SourceAddress);

                //Console.WriteLine("{0} {1}", httpRequest.GetHttpHeaderFieldByName("host"), ipPacket.SourceAddress);
                
                return;
            }

            var httpResponseMatches = Regex.Match(lines[0], @"HTTP/[0-9]\.[0-9] ([0-9]{3}) .+");

            if (httpResponseMatches.Success)
            {
                var httpResponse = new HttpResponse(httpResponseMatches.Groups[1].Value);
                httpResponse.ParseLines(lines);

                // Console.WriteLine("{0}", httpResponse.GetHttpHeaderFieldByName("content-type"));

                return;
            }
        }

        public override void AddIpUdpPacket(Packet packet, IpPacket ipPacket, UdpPacket udpPacket) { }

        public override void Start()
        {
            
        }

        public override void Stop()
        {
            
        }

        public override void AddPacket(Packet packet) { }
    }
}
